unit IfxConnectionTests;

interface

uses
  LXResourcePool,
  IfxConnection,
  TestFrameWork;


type

{ TIfxConnectionParamsTests }

  TIfxConnectionParamsTests = class(TTestCase)
  private
    function CreateParams: TIfxConnectionParams;
    procedure SetParams(Params: TIfxConnectionParams);
    function CompareParams(Params1, Params2: TIfxConnectionParams): Boolean;

  published
    procedure TestPoolOptions;
    procedure TestEquals;
    procedure TestAssign;
    procedure TestCreateClone;
    procedure TestCreateResource;
  end;


{ TIfxConnectionResourceTests }

  TIfxConnectionResourceTests = class(TTestCase)
  private
    FParams: TIfxConnectionParams;
    FConnection: TIfxConnection;
    FCollector: TLXResourceCollector;
    FResource: TIfxConnectionResource;

  protected
    procedure SetUp; override;
    procedure TearDown; override;

  published
    procedure TestAcquire;
    procedure TestMakeCurrent;
    procedure TestMakeDormant;
    procedure TestOpenDatabase;
    procedure TestCloseDatabase;
    procedure TestSetIsolation;
    procedure TestBeginTransaction;
    procedure TestCommit;
    procedure TestRollback;
    procedure TestAllocateStatement;
    procedure TestExecute;
  end;


{ TIfxStatementResourceTests }

  TIfxStatementResourceTests = class(TTestCase)
  private
    FConnParams: TIfxConnectionParams;
    FConnResource: TIfxConnectionResource;
    FResource: TIfxStatementResource;

  protected

    procedure SetUp; override;
    procedure TearDown; override;

  published

    // Test methods
    procedure TestCreate;
    procedure TestAddRefAndRelease;
    procedure TestDescribe;
    procedure TestExecute;
    procedure TestOpenClose;
    procedure TestFetch;
    procedure TestHandleDatabaseChange;

  end;


{ TIfxConnectionTests }

  TIfxConnectionTests = class(TTestCase)
  private
    FConnection: TIfxConnection;

  protected

    procedure SetUp; override;
    procedure TearDown; override;

  published

    // Test methods
    procedure TestGetAndSetInformixEnvironment;
    procedure TestGetServerNames;
    procedure TestGetHostNames;
    procedure TestGetProtocolNames;
    procedure TestAssignParams;
    procedure TestActivate;
    procedure TestMakeCurrent;
    procedure TestMakeDormant;
    procedure TestGetDatabaseNames;
    procedure TestOpenDatabase;
    procedure TestCloseDatabase;
    procedure TestStartTransaction;
    procedure TestCommit;
    procedure TestRollback;
    procedure TestApplyUpdates;
    procedure TestCloseDataSets;
    procedure TestOpenTimeOutMode;
    procedure TestCloseTimeOutMode;
    procedure TestInterruptSQL;
    procedure TestGetTableNames;
    procedure TestExecute;
    procedure TestLastInsertedSerial;
    procedure TestLastInsertedRowID;
    procedure TestLastInsertedSerial8;

  end;


implementation

uses SysUtils, Classes, IfxSqlda, IfxSqlHdr, IfxConsts, Ifx, IfxIntf;

{ TIfxConnectionParamsTests }

function TIfxConnectionParamsTests.CreateParams: TIfxConnectionParams;
begin
  Result := TIfxConnectionParams.Create;
end;

procedure TIfxConnectionParamsTests.SetParams(Params: TIfxConnectionParams);
begin
  Params.PoolOptions.Enabled := True;
  Params.PoolOptions.Min := 2;
  Params.PoolOptions.Max := 20;
  Params.PoolOptions.TimeOut := 10000;
  Params.PoolOptions.LifeTime := 100;
  Params.KeepConnection := True;
  Params.Environment.Text := 'INFROMIXSERVER=';
  Params.Database := 'test_ansi_log';
  Params.User := 'informix';
  Params.Password := 'informix';
  Params.IsolationLevel := ilCommitedRead;
  Params.SafeMode := smConcurent;
  Params.AnsiAutoCommit := True;
  Params.TransactionActionOnClose := taCommit;
  Params.CommitRetain := True;
  Params.StatementCacheSize := 50;
  Params.ShareCursorStatement := True;
end;

function TIfxConnectionParamsTests.CompareParams(Params1,
  Params2: TIfxConnectionParams): Boolean;
begin
  Result :=
    (Params1.PoolOptions.Enabled = Params2.PoolOptions.Enabled) and
    (Params1.PoolOptions.Max = Params2.PoolOptions.Max) and
    (Params1.PoolOptions.Min = Params2.PoolOptions.Min) and
    (Params1.PoolOptions.TimeOut = Params2.PoolOptions.TimeOut) and
    (Params1.PoolOptions.LifeTime = Params2.PoolOptions.LifeTime) and
    (Params1.KeepConnection = Params2.KeepConnection) and
    (Params1.Environment.Text = Params2.Environment.Text) and
    (Params1.Database = Params2.Database) and
    (Params1.User = Params2.User) and
    (Params1.Password = Params2.Password) and
    (Params1.IsolationLevel = Params2.IsolationLevel) and
    (Params1.SafeMode = Params2.SafeMode) and
    (Params1.AnsiAutoCommit = Params2.AnsiAutoCommit) and
    (Params1.TransactionActionOnClose = Params2.TransactionActionOnClose) and
    (Params1.CommitRetain = Params2.CommitRetain) and
    (Params1.StatementCacheSize = Params2.StatementCacheSize) and
    (Params1.ShareCursorStatement = Params2.ShareCursorStatement);
end;

procedure TIfxConnectionParamsTests.TestPoolOptions;
begin
  with CreateParams do
    try
      Check(PoolOptions <> nil);
    finally
      Free;
    end;
end;

procedure TIfxConnectionParamsTests.TestEquals;
var
  Params1, Params2: TIfxConnectionParams;
begin
  Params1 := nil;
  Params2 := nil;
  try
    Params1 := CreateParams;
    SetParams(Params1);
    Params2 := CreateParams;
    SetParams(Params2);

    Check(Params1.Equals(Params2), 'have to be equal');

    Params2.PoolOptions.Enabled := False;
    Check(not Params1.Equals(Params2), 'PoolOptions.Enabled diff not detected');

    SetParams(Params2);
    Params2.PoolOptions.Max := 78;
    Params2.PoolOptions.Min := 56;
    Check(not Params1.Equals(Params2), 'PoolOptions.Min/Max diff not detected');

    SetParams(Params2);
    Params2.PoolOptions.TimeOut := 55;
    Check(not Params1.Equals(Params2), 'PoolOptions.TimeOut diff not detected');

    SetParams(Params2);
    Params2.PoolOptions.LifeTime := 89;
    Check(not Params1.Equals(Params2), 'PoolOptions.LifeTime diff not detected');

    SetParams(Params2);
    Params2.KeepConnection := False;
    Check(Params1.Equals(Params2), 'KeepConnection diff not to be detected');

    SetParams(Params2);
    Params2.Environment.Text := 'another environment';
    Check(not Params1.Equals(Params2), 'Environment diff not detected');

    SetParams(Params2);
    Params2.Database := 'another database';
    Check(not Params1.Equals(Params2), 'Database diff not detected');

    SetParams(Params2);
    Params2.User := 'another user';
    Check(not Params1.Equals(Params2), 'User diff not detected');

    SetParams(Params2);
    Params2.Password := 'another password';
    Check(not Params1.Equals(Params2), 'Password diff not detected');

    SetParams(Params2);
    Params2.IsolationLevel := ilSerializable;
    Check(Params1.Equals(Params2), 'IsolationLevel diff not to be detected');

    SetParams(Params2);
    Params2.SafeMode := smPool;
    Check(Params1.Equals(Params2), 'SafeMode diff not to be detected');

    SetParams(Params2);
    Params2.AnsiAutoCommit := False;
    Check(Params1.Equals(Params2), 'AnsiAutoCommit diff not to be detected');

    SetParams(Params2);
    Params2.TransactionActionOnClose := taRollback;
    Check(Params1.Equals(Params2), 'TransactionActionOnClose diff not to be detected');

    SetParams(Params2);
    Params2.CommitRetain := False;
    Check(Params1.Equals(Params2), 'CommitRetain diff not to be detected');

    SetParams(Params2);
    Params2.StatementCacheSize := 88;
    Check(Params1.Equals(Params2), 'StatementCacheSize diff not to be detected');

    SetParams(Params2);
    Params2.ShareCursorStatement := False;
    Check(Params1.Equals(Params2), 'ShareCursorStatement diff not to be detected');
  finally
    Params1.Free;
    Params2.Free;
  end;
end;

procedure TIfxConnectionParamsTests.TestAssign;
var
  Params1, Params2: TIfxConnectionParams;
begin
  Params1 := nil;
  Params2 := nil;
  try
    Params1 := CreateParams;
    Params2 := CreateParams;
    SetParams(Params1);
    Params2.Assign(Params1);
    Check(CompareParams(Params1, Params2));
  finally
    Params1.Free;
    Params2.Free;
  end;
end;

procedure TIfxConnectionParamsTests.TestCreateClone;
var
  Params1, Params2: TIfxConnectionParams;
begin
  Params1 := nil;
  Params2 := nil;
  try
    Params1 := CreateParams;
    SetParams(Params1);
    Params2 := Params1.CreateClone as TIfxConnectionParams;
    Check(CompareParams(Params1, Params2));
  finally
    Params1.Free;
    Params2.Free;
  end;
end;

type
  TLXResourceCollectorDummy = class(TLXResourceCollector)
  private
    FResource: TLXResource;
  public
    destructor Destroy; override;
    procedure CollectResource; override;
  end;

destructor TLXResourceCollectorDummy.Destroy;
begin
  FResource.Free;
  inherited;
end;

procedure TLXResourceCollectorDummy.CollectResource;
begin
end;

procedure TIfxConnectionParamsTests.TestCreateResource;
var
  Params: TIfxConnectionParams;
  Connection: TIfxConnection;
  Collector: TLXResourceCollectorDummy;
begin
  Params := CreateParams;
  try
    SetParams(Params);
    Params.CreateResource([nil], nil).Release;
    Connection := TIfxConnection.Create(nil);
    Collector := TLXResourceCollectorDummy.Create;
    try
      Params.CreateResource([Connection], Collector).Release;
    finally
      Connection.Free;
      Collector.Free;
    end;
  finally
    Params.Free;
  end;
end;


{ TIfxConnectionResourceTests }

procedure TIfxConnectionResourceTests.SetUp;
begin
  inherited;
  FParams := TIfxConnectionParams.Create;
  FConnection := TIfxConnection.Create(nil);
  FCollector := TLXResourceCollectorDummy.Create;
  FResource := TIfxConnectionResource.Create(FCollector, FParams, [FConnection]);
  TLXResourceCollectorDummy(FCollector).FResource := FResource;
end;

procedure TIfxConnectionResourceTests.TearDown;
begin
  FResource.Release;
  FResource := nil;
  FreeAndNil(FCollector);
  FreeAndNil(FConnection);
  FreeAndNil(FParams);
  inherited;
end;

procedure TIfxConnectionResourceTests.TestAcquire;
begin
  FResource.Acquire([FConnection]);
end;

procedure TIfxConnectionResourceTests.TestMakeCurrent;
begin
  FResource.MakeCurrent;
  FResource.MakeCurrent;
end;

procedure TIfxConnectionResourceTests.TestMakeDormant;
begin
  FResource.MakeDormant;
  FResource.MakeDormant;
end;

procedure TIfxConnectionResourceTests.TestOpenDatabase;
begin
  FResource.OpenDatabase('test_ansi_log', False);
end;

procedure TIfxConnectionResourceTests.TestCloseDatabase;
begin
  FResource.OpenDatabase('test_ansi_log', False);
  FResource.CloseDatabase;
end;

procedure TIfxConnectionResourceTests.TestSetIsolation;
begin
  FResource.SetIsolation(ilDirtyRead);
end;

procedure TIfxConnectionResourceTests.TestBeginTransaction;
begin
  FResource.OpenDatabase('test_ansi_log', False);
  FResource.BeginTransaction;
end;

procedure TIfxConnectionResourceTests.TestCommit;
begin
  FResource.OpenDatabase('test_ansi_log', False);
  FResource.BeginTransaction;
  FResource.Commit;
end;

procedure TIfxConnectionResourceTests.TestRollback;
begin
  FResource.OpenDatabase('test_ansi_log', False);
  FResource.BeginTransaction;
  FResource.Rollback;
end;

procedure TIfxConnectionResourceTests.TestAllocateStatement;
begin
  FResource.OpenDatabase('test_ansi_log', False);
  FResource.AllocateStatement('select * from informix.systables', False).Release;
  FResource.AllocateStatement('select * from informix.systables', True).Release;
end;

procedure TIfxConnectionResourceTests.TestExecute;
begin
  FResource.OpenDatabase('test_ansi_log', False);
  FResource.Execute('update statistics low', nil);
end;


{ TIfxStatementResourceTests }

procedure TIfxStatementResourceTests.SetUp;
begin
  inherited;
  FConnParams := TIfxConnectionParams.Create;
  FConnParams.Database := 'test_ansi_log';
  FConnResource := TIfxConnectionResource.Create(nil, FConnParams, [nil]);
end;

procedure TIfxStatementResourceTests.TearDown;
begin
  if FResource <> nil then
    FResource.Release;
  FConnResource.Release;
  FreeAndNil(FConnParams);
  inherited;
end;

procedure TIfxStatementResourceTests.TestCreate;
begin
  try
    FResource := TIfxStatementResource.Create(FConnResource, 'asdfasd', False);
    Fail('Syntax error exception have to be');
  except
    on EIfxError do
  end;
  FreeAndNil(FResource);
  try
    FResource := TIfxStatementResource.Create(FConnResource, 'dfdfdfdf', True);
    Fail('Syntax error exception have to be');
  except
    on EIfxError do
  end;
  FreeAndNil(FResource);
  FResource := TIfxStatementResource.Create(FConnResource, 'select * from informix.systables', False);
  FreeAndNil(FResource);
  FResource := TIfxStatementResource.Create(FConnResource, 'select * from informix.systables', True);
  FreeAndNil(FResource);
end;

procedure TIfxStatementResourceTests.TestAddRefAndRelease;
begin
  FResource := TIfxStatementResource.Create(FConnResource, 'select * from informix.systables', True);
  FResource.AddRef;
  FResource.Release;
end;

procedure TIfxStatementResourceTests.TestDescribe;
var
  sqlda: PIfx_sqlda;
begin
  FResource := TIfxStatementResource.Create(FConnResource, 'select tabid from informix.systables', True);
  sqlda := FResource.Describe;
  try
    Check(sqlda <> nil);
  finally
    SqlFreeMem(sqlda, SQLDA_FREE);
  end;
end;

procedure TIfxStatementResourceTests.TestExecute;
var
  sqlda: TIfx_sqlda;
begin
  FResource := TIfxStatementResource.Create(FConnResource, 'update statistics low', True);
  FillChar(sqlda, SizeOf(sqlda), 0);
  FResource.Execute(@sqlda, nil);
end;

procedure TIfxStatementResourceTests.TestOpenClose;
var
  sqlda: TIfx_sqlda;
  Cursor: PIfx_cursor;
begin
  FResource := TIfxStatementResource.Create(FConnResource, 'select * from systables', True);
  FillChar(sqlda, SizeOf(sqlda), 0);
  FResource.Open(@sqlda, 0, Cursor);
  FResource.Close(Cursor);
end;

procedure TIfxStatementResourceTests.TestFetch;
var
  sqlda: TIfx_sqlda;
  Cursor: PIfx_cursor;
  psqlda: PIfx_sqlda;
  buffer: string;
begin
  FResource := TIfxStatementResource.Create(FConnResource, 'select tabid from systables', True);
  FillChar(sqlda, SizeOf(sqlda), 0);
  FResource.Open(@sqlda, 0, Cursor);
  try
    psqlda := FResource.Describe;
    try
      SetLength(buffer, psqlda^.sqlvar^.sqllen + 1);
      psqlda^.sqlvar^.sqldata := @buffer[1];
      FResource.Fetch(Cursor, psqlda, SCROLL_DIRECTION_NEXT, 1);
    finally
      SqlFreeMem(psqlda, SQLDA_FREE);
    end;
  finally
    FResource.Close(Cursor);
  end;
end;

procedure TIfxStatementResourceTests.TestHandleDatabaseChange;
begin
  FResource := TIfxStatementResource.Create(FConnResource, 'select tabid from systables', True);
  FConnResource.CloseDatabase;
  FResource.HandleDatabaseChange;
end;


{ TIfxConnectionTests }

procedure TIfxConnectionTests.SetUp;
begin
  FConnection := TIfxConnection.Create(nil);
  FConnection.Params.Values[szDATABASE] := 'test_buffered_log';
end;

procedure TIfxConnectionTests.TearDown;
begin
  FreeAndNil(FConnection);
end;

procedure TIfxConnectionTests.TestGetAndSetInformixEnvironment;
var
  Strings: TStrings;
begin
  Strings := TStringList.Create;
  try
    Strings.Add('WIN32USER=informix');
    TIfxConnection.SetInformixEnvironment(Strings);
  finally
    Strings.Free;
  end;

  Strings := TStringList.Create;
  try
    TIfxConnection.GetInformixEnvironment(Strings);
    Check(Strings.Values['WIN32USER'] = 'informix');
  finally
    Strings.Free;
  end;
end;

procedure TIfxConnectionTests.TestGetServerNames;
var
  Strings: TStrings;
begin
  Strings := TStringList.Create;
  try
    TIfxConnection.GetServerNames(Strings);
    Check(Strings.Count > 0); 
  finally
    Strings.Free;
  end;
end;

procedure TIfxConnectionTests.TestGetHostNames;
var
  Strings: TStrings;
begin
  Strings := TStringList.Create;
  try
    TIfxConnection.GetHostNames(Strings);
    Check(Strings.Count > 0);
  finally
    Strings.Free;
  end;
end;

procedure TIfxConnectionTests.TestGetProtocolNames;
var
  Strings: TStrings;
begin
  Strings := TStringList.Create;
  try
    TIfxConnection.GetProtocolNames(Strings);
    Check(Strings.Count > 0);
  finally
    Strings.Free;
  end;
end;

procedure TIfxConnectionTests.TestAssignParams;
begin

end;

procedure TIfxConnectionTests.TestActivate;
begin
  FConnection.Activate;
end;

procedure TIfxConnectionTests.TestMakeCurrent;
begin
  try
    FConnection.MakeCurrent;
  except
    on E: EIDACError do
  end;
  FConnection.Open;
  FConnection.MakeCurrent;
  FConnection.MakeCurrent;
end;

procedure TIfxConnectionTests.TestMakeDormant;
begin
  try
    FConnection.MakeDormant;
  except
    on E: EIDACError do
  end;
  FConnection.Open;
  FConnection.MakeDormant;
  FConnection.MakeDormant;
end;

procedure TIfxConnectionTests.TestGetDatabaseNames;
var
  Strings: TStrings;
begin
  Strings := TStringList.Create;
  try
    FConnection.GetDatabaseNames(Strings);
    Check(Strings.Count > 0);
  finally
    Strings.Free;
  end;
end;

procedure TIfxConnectionTests.TestOpenDatabase;
begin
  FConnection.OpenDatabase('sysmaster');
end;

procedure TIfxConnectionTests.TestCloseDatabase;
begin
  FConnection.OpenDatabase('sysmaster');
  FConnection.CloseDatabase;
  ExpectedException := EIfxError;
  FConnection.CloseDatabase;
end;

procedure TIfxConnectionTests.TestStartTransaction;
begin
  FConnection.StartTransaction;
  ExpectedException := EIfxError;
  FConnection.StartTransaction;
end;

procedure TIfxConnectionTests.TestCommit;
begin
  FConnection.StartTransaction;
  FConnection.Commit;
  ExpectedException := EIfxError;
  FConnection.Commit;
end;

procedure TIfxConnectionTests.TestRollback;
begin
  FConnection.StartTransaction;
  FConnection.Rollback;
  ExpectedException := EIfxError;
  FConnection.Rollback;
end;

procedure TIfxConnectionTests.TestApplyUpdates;
begin

end;

procedure TIfxConnectionTests.TestCloseDataSets;
begin

end;

procedure TIfxConnectionTests.TestOpenTimeOutMode;
begin

end;

procedure TIfxConnectionTests.TestCloseTimeOutMode;
begin

end;

procedure TIfxConnectionTests.TestInterruptSQL;
begin

end;

procedure TIfxConnectionTests.TestGetTableNames;
begin

end;

procedure TIfxConnectionTests.TestExecute;
begin

end;

procedure TIfxConnectionTests.TestLastInsertedRowID;
begin

end;

procedure TIfxConnectionTests.TestLastInsertedSerial;
begin

end;

procedure TIfxConnectionTests.TestLastInsertedSerial8;
begin

end;


initialization

  TestFramework.RegisterTest('IfxConnectionTests Suite',
    TIfxConnectionParamsTests.Suite);
  TestFramework.RegisterTest('IfxConnectionTests Suite',
    TIfxConnectionResourceTests.Suite);
  TestFramework.RegisterTest('IfxConnectionTests Suite',
    TIfxStatementResourceTests.Suite);
  TestFramework.RegisterTest('IfxConnectionTests Suite',
    TIfxConnectionTests.Suite);

end.
 